﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    [RequiresAuthentication]
    public class ProposalController : ComperioController
    {
        private ProposalBLL proposalBLL;

        public ProposalController()
        {
            proposalBLL = new ProposalBLL();            
        }

        public ProposalController(ProposalBLL proposalbll)
        {
            this.proposalBLL = proposalbll;
        }
        
        #region Index

        [HttpGet]
        public ActionResult Index()
        {
            ViewData.Model = proposalBLL.RetrieveProposals();            
            return View();
        }

        #endregion

        #region MyIndex
        
        [HttpGet]
        public ActionResult MyIndex()
        {
            //ViewData["GridSource"] = proposalBLL.RetrieveProposal(Convert.ToInt32(this.CurrentUser.PersonID));
            ViewData.Model = proposalBLL.RetrieveProposals(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }

        #endregion
        
        #region Create

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Proposal proposal)
        {
            if (!ModelState.IsValid)
                return View(proposal);


            proposal.AcademicPersonID = Convert.ToInt32(Convert.ToInt32(this.CurrentUser.PersonID));            
            proposalBLL.Create(proposal);
                        
            return RedirectToAction("MyIndex");
        }

        #endregion
                
        #region Edit

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(String id)
        {
            Session["ProposalID"] = id;
            var proposal = proposalBLL.RetrieveProposal(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(proposal);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "id")] Proposal _proposal)
        {
            if (!ModelState.IsValid)
                return View(_proposal);

            var id = URLEncrypt.DecryptFromString(Convert.ToString(Session["ProposalID"]));
            var proposal = proposalBLL.RetrieveProposal(Convert.ToInt32(id));

            proposal.Title = _proposal.Title;
            proposal.Description = _proposal.Description;

            proposalBLL.Edit(proposal);

            return RedirectToAction("MyIndex");
        }

        #endregion

        #region Details

        [HttpGet]
        public ActionResult Details(string id)
        {
            var entity = proposalBLL.RetrieveProposal(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }

        #endregion
                
        #region Vizualize

        [HttpGet]
        public ActionResult Vizualize(string id)
        {
            var entity = proposalBLL.RetrieveProposal(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }

        #endregion
                
        #region Delete
        [HttpGet]
        public ActionResult Delete(String id)
        {
            proposalBLL.Remove(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));

            return RedirectToAction("MyIndex");
        }

        #endregion

    }
}
